Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

npm audit docs tests and bpe fix + countTokens properly added #18

Closed
wants to merge 34 commits into from

Conversation

syonfox
Copy link

@syonfox syonfox commented Dec 25, 2022

Well, I went to use this and I didn't finish implementing it last time. you need to add the function to the index.

I spend some time updating the tests as well as adding a random test.

Hopefully, this is a little better. if the maintainer is interested I can do a little more cleanup n such but overall I think the next step is to look into that quote bug. / write a script to get the different bpe encodings from openai.

https://syonfox.github.io/GPT-3-Encoder/global.html#encode

Hope this helps

Merry Christmas

syonfox and others added 30 commits December 19, 2022 22:27
I don't actually want to encode into tokens for my use case, quickly count to check my request won't exceed the limit.

This should be faster since we don't initialize the memory for the output array.

```
const crypto = require('crypto');
// Generate a random string of a given length
function generateRandomString(length) {
    return crypto.randomBytes(length).toString('hex');
}

const {encode, decode, countTokens} = require('gpt-3-encoder')

let str = 'This is an example sentence to try encoding out on!'
// let now = Date.now();
let encoded = encode(str)
console.log('Encoded this string looks like: ', encoded)
console.log('We can look at each token and what it represents)
let tokencount = 0;
for(let token of encoded){
    tokencount ++;
    console.log({token, string: decode([token])})
}
console.log("there are n tokens: ", tokencount);
let decoded = decode(encoded)
console.log('We can decode it back into:\n', decoded)

let now = Date.now();
// todo: write an benchmark for the above method vs  int countTokens(str)
str = generateRandomString(10000);

console.time('fencode');
encoded = encode(str);
console.log(`First encode to cache string n stuff in mem`);
console.timeEnd('fencode');


console.log(`Original string length: ${str.length}`);
// Benchmark the encode function
console.time('encode');
encoded = encode(str);
console.log(`Encoded string length: ${encoded.length}`);
console.timeEnd('encode');

// Benchmark the countTokens function
console.time('countTokens');
let tokenCount = countTokens(str);
console.log(`Number of tokens: ${tokenCount}`);
console.timeEnd('countTokens');


console.log(`Original string length: ${str.length}`);
console.log(`Encoded string length: ${encoded.length}`);
console.log(`Number of tokens: ${tokenCount}`);
```

```
We can decode it back into:
 This is an example sentence to try encoding out on!
First encode to cache string n stuff in mem
fencode: 163.57ms
Original string length: 20000
Encoded string length: 11993
encode: 124.265ms
Number of tokens: 11993
countTokens: 29.2ms
Original string length: 20000
Encoded string length: 11993
Number of tokens: 11993

```
Co-authored-by: Andrew Healey <[email protected]>
I don't actually want to encode into tokens for my use case, quickly count to check my request won't exceed the limit.

This should be faster since we don't initialize the memory for the output array.

```
const crypto = require('crypto');
// Generate a random string of a given length
function generateRandomString(length) {
    return crypto.randomBytes(length).toString('hex');
}

const {encode, decode, countTokens} = require('gpt-3-encoder')

let str = 'This is an example sentence to try encoding out on!'
// let now = Date.now();
let encoded = encode(str)
console.log('Encoded this string looks like: ', encoded)
console.log('We can look at each token and what it represents)
let tokencount = 0;
for(let token of encoded){
    tokencount ++;
    console.log({token, string: decode([token])})
}
console.log("there are n tokens: ", tokencount);
let decoded = decode(encoded)
console.log('We can decode it back into:\n', decoded)

let now = Date.now();
// todo: write an benchmark for the above method vs  int countTokens(str)
str = generateRandomString(10000);

console.time('fencode');
encoded = encode(str);
console.log(`First encode to cache string n stuff in mem`);
console.timeEnd('fencode');


console.log(`Original string length: ${str.length}`);
// Benchmark the encode function
console.time('encode');
encoded = encode(str);
console.log(`Encoded string length: ${encoded.length}`);
console.timeEnd('encode');

// Benchmark the countTokens function
console.time('countTokens');
let tokenCount = countTokens(str);
console.log(`Number of tokens: ${tokenCount}`);
console.timeEnd('countTokens');


console.log(`Original string length: ${str.length}`);
console.log(`Encoded string length: ${encoded.length}`);
console.log(`Number of tokens: ${tokenCount}`);
```

```
We can decode it back into:
 This is an example sentence to try encoding out on!
First encode to cache string n stuff in mem
fencode: 163.57ms
Original string length: 20000
Encoded string length: 11993
encode: 124.265ms
Number of tokens: 11993
countTokens: 29.2ms
Original string length: 20000
Encoded string length: 11993
Number of tokens: 11993

```

Co-authored-by: Kier <[email protected]>
merge back the one other change and follow nick with moving to version 1.2 for feature add.
…e updates

maybe we should host jsdoc on github pages

also performed npm audit and updates for security vulnerabilities.
It seems to work fine
By using the Buffer class in this way, you can encode and decode strings as UTF-8 without using the TextEncoder and TextDecoder classes. This can be useful if you need to support earlier versions of Node.js that do not have these classes in the util module.
…eaking change but i think its actually mor widely compatible and should be fine.
…by webpaking... todo need to test browser version

This precomputes bpe_ranks so that it is it the from needed make sure to rebuild if you change the bpe dump file
@syonfox
Copy link
Author

syonfox commented Jan 19, 2023

Hmm this did keep going a bit further closing see #17 or #30

@syonfox syonfox closed this Jan 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants